package jiinn.controllers;

import jiinn.interfaces.MemberService;
import jiinn.interfaces.ZipcodeService;
import jiinn.models.MemberVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/join/*")
public class JoinController {
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private ZipcodeService zipcodeService;
	
	@RequestMapping("/agree")
	public String agree() throws Exception {
		return "join/agree";
	}	
	
	@RequestMapping(value="/joincheck", method=RequestMethod.GET)
	public String joincheck() throws Exception {
		return "join/joincheck";
	}
	@RequestMapping(value="/joincheck", method=RequestMethod.POST)
	public String joincheck(@ModelAttribute MemberVO m, Model model) throws Exception {
		String[] emails = m.getEmail().split("@");
		model.addAttribute("chkname", m.getName());
		model.addAttribute("email1", emails[0]);
		model.addAttribute("email2", emails[1]);
		return memberService.joinChkMember(m);
	}
	
	@RequestMapping("/checkuid")
	@ResponseBody
	public String checkuid(@RequestParam String uid) throws Exception {
		return memberService.checkUid(uid);
	}
	
	@RequestMapping("/joinalready")
	public String joinalready(@ModelAttribute MemberVO m) throws Exception {
		return "join/joinalready";
	}
	
	@RequestMapping(value="/joininfo", method=RequestMethod.GET)
	public String joininfo() throws Exception {
		return "join/joininfo";
	}
	@RequestMapping(value="/joininfo", method=RequestMethod.POST)
	public String joininfo(@ModelAttribute MemberVO m) throws Exception {
		return memberService.newMember(m);
	}
	
	@RequestMapping(value="/joinok", method=RequestMethod.GET)
	public String joinok() throws Exception {
		return "join/joinok";
	}
	
	@RequestMapping(value="/zipcode", produces="text/plain; charset=UTF-8")
	@ResponseBody
	public String zipcode(@RequestParam String dong) throws Exception {
		return zipcodeService.readZipcode(dong);
	}

}
